package com.yiren.dbaa.module.practice_set.line.model;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.utils.DateUtil;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.contract.LineReleaseContract;
import com.yiren.dbaa.net.OkHttpUtils;
import com.yiren.dbaa.net.bean.TestBaseBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lwb
 * @date : 2022/11/16 17:11
 * @description: 线路发布
 */
public class LineReleaseModel implements LineReleaseContract.Model {
    private Activity activity;
    private Gson gson;
    private UserParamConfigBean userParamConfigBean;

    public LineReleaseModel(Activity activity) {
        this.activity = activity;
        gson = new Gson();
        userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void publishLine(LineInfoEntity lineInfo, String userId, String updateDetails, int updateMode, CommonCallBack callBack) {
        List<LinePointBean>  linePointList = new Gson().fromJson(lineInfo.getPartneModifyVal(),new TypeToken<List<LinePointBean>>() {}.getType());
        int pointNumber = 0;
        if (linePointList!=null) pointNumber = linePointList.size();
        Map<String, Object> map = new HashMap<>();
        map.put("id",lineInfo.getId());
        map.put("siteCode", lineInfo.getSiteCode());
        map.put("name",lineInfo.getName());
        map.put("lineType",lineInfo.getLineType());
        map.put("userId", userId);
        map.put("lineVer", lineInfo.getLineVerLast()+1);
        map.put("useRange", lineInfo.getUseRange());
        map.put("updateDetails", updateDetails);
        map.put("updateMode", updateMode);
        map.put("publishTime", DateUtil.getCurrentDate());
        map.put("state", 1);
        map.put("location",lineInfo.getLocation());
        map.put("pointNumber",pointNumber);
        if (lineInfo.getPartneModifyVal() == null)
            lineInfo.setPartneModifyVal("");
        map.put("modifyVal",lineInfo.getPartneModifyVal());

        new Thread(new Runnable() {
            @Override
            public void run() {
                String line = OkHttpUtils.getInstance().z(AddressConstant.publishLine().getUrl(),new Gson().toJson(map));
                if (!StringUtil.isEmpty(line)) {
                    TestBaseBean<String> testBaseBean = gson.fromJson(line, new TypeToken<TestBaseBean<String>>() {
                    }.getType());
                    if (testBaseBean.isSuccess()) {
                        callBack.success(testBaseBean.getData());
                    } else {
                        callBack.fail("发布失败!");
                    }
                }else {
                    callBack.fail("发布失败!");
                }
            }
        }).start();
    }
}
